﻿

/* timeline-view.js */
Exhibit.TimelineView=function(C,B){this._div=C;
this._uiContext=B;
this._settings={};
this._accessors={getEventLabel:function(F,E,D){D(E.getObject(F,"label"));
},getProxy:function(F,E,D){D(F);
},getColorKey:null,getIconKey:null};
this._selectListener=null;
this._largestSize=0;
var A=this;
this._listener={onItemsChanged:function(){A._reconstruct();
}};
B.getCollection().addListener(this._listener);
};
Exhibit.TimelineView._intervalChoices=["millisecond","second","minute","hour","day","week","month","year","decade","century","millennium"];
Exhibit.TimelineView._settingSpecs={"topBandHeight":{type:"int",defaultValue:75},"topBandUnit":{type:"enum",choices:Exhibit.TimelineView._intervalChoices},"topBandPixelsPerUnit":{type:"int",defaultValue:200},"bottomBandHeight":{type:"int",defaultValue:25},"bottomBandUnit":{type:"enum",choices:Exhibit.TimelineView._intervalChoices},"bottomBandPixelsPerUnit":{type:"int",defaultValue:200},"timelineHeight":{type:"int",defaultValue:400},"timelineConstructor":{type:"function",defaultValue:null},"colorCoder":{type:"text",defaultValue:null},"iconCoder":{type:"text",defaultValue:null},"selectCoordinator":{type:"text",defaultValue:null},"showHeader":{type:"boolean",defaultValue:true},"showSummary":{type:"boolean",defaultValue:true},"showFooter":{type:"boolean",defaultValue:true}};
Exhibit.TimelineView._accessorSpecs=[{accessorName:"getProxy",attributeName:"proxy"},{accessorName:"getDuration",bindings:[{attributeName:"start",type:"date",bindingName:"start"},{attributeName:"end",type:"date",bindingName:"end",optional:true}]},{accessorName:"getColorKey",attributeName:"marker",type:"text"},{accessorName:"getColorKey",attributeName:"colorKey",type:"text"},{accessorName:"getIconKey",attributeName:"iconKey",type:"text"},{accessorName:"getEventLabel",attributeName:"eventLabel",type:"text"}];
Exhibit.TimelineView.create=function(D,C,B){var A=new Exhibit.TimelineView(C,Exhibit.UIContext.create(D,B));
Exhibit.TimelineView._configure(A,D);
A._internalValidate();
A._initializeUI();
return A;
};
Exhibit.TimelineView.createFromDOM=function(D,C,B){var E=Exhibit.getConfigurationFromDOM(D);
var A=new Exhibit.TimelineView(C!=null?C:D,Exhibit.UIContext.createFromDOM(D,B));
Exhibit.SettingsUtilities.createAccessorsFromDOM(D,Exhibit.TimelineView._accessorSpecs,A._accessors);
Exhibit.SettingsUtilities.collectSettingsFromDOM(D,Exhibit.TimelineView._settingSpecs,A._settings);
Exhibit.TimelineView._configure(A,E);
A._internalValidate();
A._initializeUI();
return A;
};
Exhibit.TimelineView._configure=function(A,C){Exhibit.SettingsUtilities.createAccessors(C,Exhibit.TimelineView._accessorSpecs,A._accessors);
Exhibit.SettingsUtilities.collectSettings(C,Exhibit.TimelineView._settingSpecs,A._settings);
var B=A._accessors;
A._getDuration=function(F,E,D){B.getProxy(F,E,function(G){B.getDuration(G,E,D);
});
};
};
Exhibit.TimelineView.prototype.dispose=function(){this._uiContext.getCollection().removeListener(this._listener);
this._timeline=null;
if(this._selectListener!=null){this._selectListener.dispose();
this._selectListener=null;
}this._toolboxWidget.dispose();
this._toolboxWidget=null;
this._dom.dispose();
this._dom=null;
this._div.innerHTML="";
this._div=null;
this._uiContext.dispose();
this._uiContext=null;
};
Exhibit.TimelineView.prototype._internalValidate=function(){if("getColorKey" in this._accessors){if("colorCoder" in this._settings){this._colorCoder=this._uiContext.getExhibit().getComponent(this._settings.colorCoder);
}if(this._colorCoder==null){this._colorCoder=new Exhibit.DefaultColorCoder(this._uiContext);
}}if("getIconKey" in this._accessors){this._iconCoder=null;
if("iconCoder" in this._settings){this._iconCoder=this._uiContext.getExhibit().getComponent(this._settings.iconCoder);
}}if("selectCoordinator" in this._settings){var B=exhibit.getComponent(this._settings.selectCoordinator);
if(B!=null){var A=this;
this._selectListener=B.addListener(function(C){A._select(C);
});
}}};
Exhibit.TimelineView.prototype._initializeUI=function(){var A=this;
var B={};
B.colorGradient=(this._colorCoder!=null&&"_gradientPoints" in this._colorCoder);
B.iconMarkerGenerator=function(C){elmt=document.createElement("img");
elmt.src=C;
elmt.style.verticalAlign="middle";
return elmt;
};
this._div.innerHTML="";
this._dom=Exhibit.ViewUtilities.constructPlottingViewDom(this._div,this._uiContext,this._settings.showSummary&&this._settings.showHeader,{onResize:function(){A._timeline.layout();
}},B);
this._toolboxWidget=Exhibit.ToolboxWidget.createFromDOM(this._div,this._div,this._uiContext);
this._eventSource=new Timeline.DefaultEventSource();
this._reconstruct();
};
Exhibit.TimelineView.prototype._reconstructTimeline=function(A){var E=this._settings;
if(this._timeline!=null){this._timeline.dispose();
}if(A){this._eventSource.addMany(A);
}var N=this._dom.plotContainer;
if(E.timelineConstructor!=null){this._timeline=E.timelineConstructor(N,this._eventSource);
}else{N.style.height=E.timelineHeight+"px";
N.className="exhibit-timelineView-timeline";
var K=Timeline.ClassicTheme.create();
K.event.bubble.width=this._uiContext.getSetting("bubbleWidth");
K.event.bubble.height=this._uiContext.getSetting("bubbleHeight");
var G,M;
if(E.topBandUnit!=null||E.bottomBandUnit!=null){if(Exhibit.TimelineView._intervalLabelMap==null){Exhibit.TimelineView._intervalLabelMap={"millisecond":Timeline.DateTime.MILLISECOND,"second":Timeline.DateTime.SECOND,"minute":Timeline.DateTime.MINUTE,"hour":Timeline.DateTime.HOUR,"day":Timeline.DateTime.DAY,"week":Timeline.DateTime.WEEK,"month":Timeline.DateTime.MONTH,"year":Timeline.DateTime.YEAR,"decade":Timeline.DateTime.DECADE,"century":Timeline.DateTime.CENTURY,"millennium":Timeline.DateTime.MILLENNIUM};
}if(E.topBandUnit==null){M=Exhibit.TimelineView._intervalLabelMap[E.bottomBandUnit];
G=M-1;
}else{if(E.bottomBandUnit==null){G=Exhibit.TimelineView._intervalLabelMap[E.topBandUnit];
M=G+1;
}else{G=Exhibit.TimelineView._intervalLabelMap[E.topBandUnit];
M=Exhibit.TimelineView._intervalLabelMap[E.bottomBandUnit];
}}}else{var L=this._eventSource.getEarliestDate();
var H=this._eventSource.getLatestDate();
var O=H.getTime()-L.getTime();
var F=this._eventSource.getCount();
if(O>0&&F>1){var C=F/O;
var G=Timeline.DateTime.MILLENNIUM;
while(G>0){var I=Timeline.DateTime.gregorianUnitLengths[G];
var D=C*I/E.topBandPixelsPerUnit;
if(D<0.01){break;
}G--;
}}else{G=Timeline.DateTime.YEAR;
}M=G+1;
}var Q=[Timeline.createBandInfo({width:E.topBandHeight+"%",intervalUnit:G,intervalPixels:E.topBandPixelsPerUnit,eventSource:this._eventSource,theme:K}),Timeline.createBandInfo({width:E.bottomBandHeight+"%",intervalUnit:M,intervalPixels:E.bottomBandPixelsPerUnit,eventSource:this._eventSource,overview:true,theme:K})];
Q[1].syncWith=0;
Q[1].highlight=true;
this._timeline=Timeline.create(N,Q,Timeline.HORIZONTAL);
}var P=this;
var B=function(R){if(P._selectListener!=null){P._selectListener.fire({itemIDs:[R]});
}};
for(var J=0;
J<this._timeline.getBandCount();
J++){this._timeline.getBand(J).getEventPainter().addOnSelectListener(B);
}};
Exhibit.TimelineView.prototype._reconstruct=function(){var O=this;
var N=this._uiContext.getCollection();
var L=this._uiContext.getDatabase();
var X=this._settings;
var Y=this._accessors;
var B=N.countRestrictedItems();
var P=[];
this._dom.legendWidget.clear();
this._eventSource.clear();
if(B>0){var C=N.getRestrictedItems();
var E=(this._accessors.getColorKey!=null);
var D=(this._accessors.getIconKey!=null&&this._iconCoder!=null);
var H={mixed:false,missing:false,others:false,keys:new Exhibit.Set()};
var Q={mixed:false,missing:false,others:false,keys:new Exhibit.Set()};
var A=[];
var R=function(g,f,c,e){var d;
Y.getEventLabel(g,L,function(h){d=h;
return true;
});
var b=new Timeline.DefaultEventSource.Event(g,f.start,f.end,null,null,f.end==null,d,"",null,null,e,c,c);
b._itemID=g;
b.getProperty=function(h){return L.getObject(this._itemID,h);
};
b.fillInfoBubble=function(h,i,j){O._fillInfoBubble(this,h,i,j);
};
A.push(b);
};
C.visit(function(h){var d=[];
O._getDuration(h,L,function(i){if("start" in i){d.push(i);
}});
if(d.length>0){var c=null;
var g=null;
if(E){var e=new Exhibit.Set();
Y.getColorKey(h,L,function(i){e.add(i);
});
c=O._colorCoder.translateSet(e,H);
}var g=null;
if(D){var b=new Exhibit.Set();
Y.getIconKey(h,L,function(i){b.add(i);
});
g=O._iconCoder.translateSet(b,Q);
}for(var f=0;
f<d.length;
f++){R(h,d[f],c,g);
}}else{P.push(h);
}});
if(E){var V=this._dom.legendWidget;
var U=this._colorCoder;
var K=H.keys.toArray().sort();
if(this._colorCoder._gradientPoints!=null){V.addGradient(this._colorCoder._gradientPoints);
}else{for(var T=0;
T<K.length;
T++){var a=K[T];
var S=U.translate(a);
V.addEntry(S,a);
}}if(H.others){V.addEntry(U.getOthersColor(),U.getOthersLabel());
}if(H.mixed){V.addEntry(U.getMixedColor(),U.getMixedLabel());
}if(H.missing){V.addEntry(U.getMissingColor(),U.getMissingLabel());
}}if(D){var V=this._dom.legendWidget;
var G=this._iconCoder;
var K=Q.keys.toArray().sort();
if(X.iconLegendLabel!=null){V.addLegendLabel(X.iconLegendLabel,"icon");
}for(var T=0;
T<K.length;
T++){var a=K[T];
var W=G.translate(a);
V.addEntry(W,a,"icon");
}if(Q.others){V.addEntry(G.getOthersIcon(),G.getOthersLabel(),"icon");
}if(Q.mixed){V.addEntry(G.getMixedIcon(),G.getMixedLabel(),"icon");
}if(Q.missing){V.addEntry(G.getMissingIcon(),G.getMissingLabel(),"icon");
}}var I=B-P.length;
if(I>this._largestSize){this._largestSize=I;
this._reconstructTimeline(A);
}else{this._eventSource.addMany(A);
}var M=this._timeline.getBand(0);
var F=M.getCenterVisibleDate();
var J=this._eventSource.getEarliestDate();
var Z=this._eventSource.getLatestDate();
if(J!=null&&F<J){M.scrollToCenter(J);
}else{if(Z!=null&&F>Z){M.scrollToCenter(Z);
}}}this._dom.setUnplottableMessage(B,P);
};
Exhibit.TimelineView.prototype._select=function(A){var B=A.itemIDs[0];
this._timeline.getBand(0).showBubbleForEvent(B);
};
Exhibit.TimelineView.prototype._fillInfoBubble=function(A,B,C,D){this._uiContext.getLensRegistry().createLens(A._itemID,B,this._uiContext);
};
